package gui.common;

import gui.GuiCommon;

import gui.client.listener.ClientButtonListener;
import gui.common.detail.DetailPage;
import gui.common.detail.ForumDetail;
import gui.common.detail.GroupDetail;
import gui.common.detail.MessageDetail;
import gui.common.detail.UserDetail;
import gui.common.msg.MsgType;
import gui.common.msg.SysMessage;
import gui.server.Server;

import java.awt.event.ActionListener;
import java.util.List;

import client.ChatClient;
import data.Forum;
import data.Group;
import data.Message;
import data.User;

public class CommonListener{
	public static void createForum(BaseClientFrame client,DetailPage src,ActionListener lis){
		SysMessage msg=new SysMessage();
		msg.subject="Create new Forum";
		User user=client.getDataClient().getCurrentUser();
		Forum forum=(Forum)src.getOperationResult();
		Group group=client.getDataClient().getCurrentGroup();
    	if(forum.getId()>0){
    		msg.detail="Error,it's a exists forum";
    	}else if(forum.getSubject()==null||forum.getSubject().length()<1){
    		msg.detail="Please enter forum subject";
    	}else if(forum.getMessage()==null||forum.getMessage().length()<1){
    		msg.detail="Please enter forum message";
    	}else{
    		forum.setUserId(user.getId());
    		try {
    			msg=client.getDataClient().getRemoteServer().createNewForum(forum);
    			if(msg.data!=null){
    				if(src!=null){
    					src.dispose();
    				}
    				Forum newForum=(Forum)msg.data;
    				msg.type=MsgType.Success;
    				msg.detail="Create New Forum:"+"\nName:"+newForum.getSubject()
    				+"\nDate"+newForum.getDatetimeString();
    				
    				
    			}
			} catch (Exception e) {
				e.printStackTrace();
				msg.detail=e.getMessage();
				msg.type=MsgType.Error;
			}
    	}
    	GuiCommon.showAlertWindow(msg);
    	try {
			List<Forum> data=client.getDataClient().getRemoteServer().getForumList(group);
			ForumListPanel forumListPanel=new ForumListPanel(group,data,lis);
			client.setContent(forumListPanel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void createMessage(BaseClientFrame client,DetailPage src,ActionListener al){
		SysMessage msg=new SysMessage();
		msg.subject="Create new Message";
		User user=client.getDataClient().getCurrentUser();
		Message message=(Message)src.getOperationResult();
		Forum forum=client.getDataClient().getCurrentForum();
    	if(message.getId()>0){
    		msg.detail="Error,it's a exists forum";
    	}else if(message.getSubject()==null||message.getSubject().length()<1){
    		msg.detail="Please enter forum subject";
    	}else if(message.getMessage()==null||message.getMessage().length()<1){
    		msg.detail="Please enter forum message";
    	}else{
    		message.setUserId(user.getId());
    		try {
    			msg=client.getDataClient().getRemoteServer().createNewMessage(message);
    			if(msg.data!=null){
    				if(src!=null){
    					src.dispose();
    				}
    				Message newMessage=(Message)msg.data;
    				msg.type=MsgType.Success;
    				msg.detail="Create New Message:"+"\nName:"+newMessage.getSubject()
    				+"\nDate"+newMessage.getDatetimeString();
    				
    				
    			}
			} catch (Exception e) {
				e.printStackTrace();
				msg.detail=e.getMessage();
				msg.type=MsgType.Error;
			}
    	}
    	GuiCommon.showAlertWindow(msg);
    	try {
			List<Message> data=client.getDataClient().getRemoteServer().getMessageList(forum);
			MessageListPanel forumListPanel=new MessageListPanel(forum,data,al);
			if(client instanceof Server){
				forumListPanel.showAdminButtons();
			}
			client.setContent(forumListPanel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void enterGroup(BaseClientFrame client,ActionListener al){
		SysMessage msg=new SysMessage();
		msg.subject="Join Group";
		User user=client.getDataClient().getCurrentUser();
    	if(user==null){
    		msg.detail="You must login first";
    	}else{
    		DefaultGroupList groupListPanel=(DefaultGroupList)client.getContent();
    		Group g=groupListPanel.getCurrentGroup();
    		if(g==null){
    			msg.detail="Please select a group from left";
    		}else{
    			client.getDataClient().setCurrentGroup(g);
    			try {
    				List<Forum> data=client.getDataClient().getRemoteServer().getForumList(g);
    				ForumListPanel forumListPanel=new ForumListPanel(g,data,al);
    				client.setContent(forumListPanel);
				} catch (Exception e) {
					e.printStackTrace();
				}
    		}
    	}
    	if(msg.type==MsgType.Error){
    		GuiCommon.showAlertWindow(msg);
    	}
	}
	public static void createAccount(BaseClientFrame client,DetailPage src){
		if(client.isConnected()){
    		UserDetail userDetail=(UserDetail)src;
    		String username=userDetail.getUserName();
    		String password=userDetail.getPassword();
    		SysMessage result=new SysMessage();
    		result.subject="Create user account";
    		try {
				result=client.getDataClient().getRemoteServer().createUser(username, password);
				if(result.data!=null){
					User user=(User)result.data;
					client.getDataClient().setCurrentUser(user);
					src.dispose();
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.type=MsgType.Error;
				result.detail=e.getMessage();
				result.subject="create user";
			}
			GuiCommon.showAlertWindow(result);
    	}
	}
	
	public static void createGroup(BaseClientFrame client,DetailPage src){
		SysMessage msg=new SysMessage();
		msg.subject="Create new Group";
		User user=client.getDataClient().getCurrentUser();
		Group group=(Group)src.getOperationResult();
    	if(group.getId()>0){
    		msg.detail="Error,it's a exists group";
    	}else if(group.getName()==null||group.getName().length()<1){
    		msg.detail="Please enter group name";
    	}else if(group.getDescription()==null||group.getDescription().length()<1){
    		msg.detail="Please enter group description";
    	}else{
    		group.setCreaterId(user.getId());
    		try {
    			msg=client.getDataClient().getRemoteServer().createNewGroup(group);
    			if(msg.data!=null){
    				if(src!=null){
    					src.dispose();
    				}
    				Group newGroup=(Group)msg.data;
    				msg.type=MsgType.Success;
    				msg.detail="Create Group Resquest:"+"\nName:"+newGroup.getName()
    				+"\nDate"+newGroup.getDateString();
    				
    			}
			} catch (Exception e) {
				e.printStackTrace();
				msg.detail=e.getMessage();
				msg.type=MsgType.Error;
			}
    	}
    	GuiCommon.showAlertWindow(msg);
	}
	public static void startChat(BaseClientFrame client){
		User user=client.getDataClient().getCurrentUser();
    	Group group=client.getDataClient().getCurrentGroup();
    	SysMessage msg=new SysMessage();
    	if(user==null||group==null){
    		
    		msg.subject="Live Chat";
    		msg.detail="Sorry ,the user info or group info error";
    	}else{
    		try {
    			new ChatClient(user.getUsername(),group);
    			msg.type=MsgType.Success;
    		} catch (Exception e2) {
    			msg.detail=e2.getLocalizedMessage();
    			msg.type=MsgType.Error;
    			e2.printStackTrace();
			}
    	}
    	if(msg.type!=MsgType.Success){
    		GuiCommon.showAlertWindow(msg);
    	}
	}
	
	public static void leaveGroup(BaseClientFrame client,ActionListener al){
		SysMessage msg=new SysMessage();
    	try {
    		List<Group> groupInfo=null;
    		DefaultGroupList gp=null;
    		String welcome=client.getDataClient().getRemoteServer().getWelcomeInfo();
    		groupInfo=client.getDataClient().getRemoteServer().getGroupList(false);
    		gp=new DefaultGroupList(groupInfo,al,welcome);
    		gp.showButtonAfterLogin();
    		client.setContent(gp);
    		client.getDataClient().setCurrentGroup(null);
    		msg.type=MsgType.Success;
		} catch (Exception e2) {
			e2.printStackTrace();
			msg.detail=msg.detail+"\n"+e2.getMessage();
		}
    	if(msg.type!=MsgType.Success){
    		GuiCommon.showAlertWindow(msg);
    	}
	}
	
	public static void enterForum(BaseClientFrame client,ActionListener al){
		SysMessage msg=new SysMessage();
		msg.subject="View Forum";
		ForumListPanel forumListPanel=(ForumListPanel)client.getContent();
    	Forum forum=forumListPanel.getCurrentForum();
    	if(forum==null){
    			msg.detail="Please select a forum from left";
    		}else{
    			client.getDataClient().setCurrentForum(forum);
    			try {
    				List<Message> data=client.getDataClient().getRemoteServer().getMessageList(forum);
    				MessageListPanel messageListPanel=new MessageListPanel(forum,data,al);
    				if(client instanceof Server){
    					messageListPanel.showAdminButtons();
    				}
    				client.setContent(messageListPanel);
    				msg.type=MsgType.Success;
				} catch (Exception e) {
					e.printStackTrace();
				}
    		}
    	if(msg.type!=MsgType.Success){
    		GuiCommon.showAlertWindow(msg);
    	}
	}
	
	public static void leaveForum(BaseClientFrame client,ActionListener al){
		Group g=client.getDataClient().getCurrentGroup();
    	try {
    		List<Forum> data=client.getDataClient().getRemoteServer().getForumList(g);
    		ForumListPanel forumListPanel=new ForumListPanel(g,data,al);
    		client.setContent(forumListPanel);
    		client.getDataClient().setCurrentForum(null);
    	} catch (Exception e) {
    		e.printStackTrace();
		}
	}
	
	public static void createGroup(BaseClientFrame client,boolean isServer){
		SysMessage msg=new SysMessage();
		msg.subject="Create Group";
		User user=client.getDataClient().getCurrentUser();
    	if(user==null){
    		msg.detail="You must login first";
    	}else{
    		Group group=new Group();
    		group.setCreaterName(user.getUsername());
    		GroupDetail groupDetail=new GroupDetail(true,group,isServer);
    		groupDetail.setVisible(true);
    	}
	}
	
	public static void createForum(BaseClientFrame client,boolean isServer){
		SysMessage msg=new SysMessage();
		msg.subject="Create Forum";
		User user=client.getDataClient().getCurrentUser();
		Group group=client.getDataClient().getCurrentGroup();
    	if(group==null){
    		msg.detail="You must login first";
    	}else{
    		Forum forum=new Forum();
    		forum.setGroupId(group.getId());
    		forum.setUserId(user.getId());
    		forum.setDelete(1);
    		forum.setUserName(user.getUsername());
    		ForumDetail forumDetail=new ForumDetail(true,forum,isServer);
    		forumDetail.setVisible(true);
    	}
	}
	
	public static void createMeasage(BaseClientFrame client,boolean isServer){
		SysMessage msg=new SysMessage();
		msg.subject="Create Message";
		User user=client.getDataClient().getCurrentUser();
		Forum forum=client.getDataClient().getCurrentForum();
    	if(forum==null){
    		msg.detail="You must login first";
    	}else{
    		Message message=new Message();
    		message.setForumId(forum.getId());
    		message.setUserId(user.getId());
    		message.setDelete(1);
    		message.setUserName(user.getUsername());
    		MessageDetail messageDetail=new MessageDetail(true,message,isServer);
    		messageDetail.setVisible(true);
    	}
	}
	
	

}
